package com.zhongyi.service.impl;

import com.zhongyi.dao.AccountDao;
import com.zhongyi.domian.Account;
import com.zhongyi.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly = false)
    public void transform(Integer targetID, Integer sourceID, float money) {
        Account tar = accountDao.findAccountById(targetID);

        Account sour = accountDao.findAccountById(sourceID);

        tar.setMoney(tar.getMoney()-100);
        sour.setMoney(sour.getMoney()+100);

        accountDao.updateAccount(tar);

        int i = 1/0;

        accountDao.updateAccount(sour);
        System.out.println(tar);
        System.out.println(sour);

    }
}
